function  stdp_plots()
%
% generate some summary graphs for spike-timing dependent potentioniation
%

global TRES EPSC_STATE CONTROL
persistent next_color

dt1 = -5;
dt2 = -15;
if(dt2<dt1)
   t = dt2;
   dt2=dt1;
   dt1=t;
end;
mindt=dt1;
maxdt=dt2;
ESPC_STATE=1;

if(isempty(next_color))
   next_color = 1;
end;
clist = {'r', 'g', 'b', 'y', 'k'};
mlist = {'o', 's', '^', 'x', 'd'};
sf = getmainselection;
hctl = findobj('Tag', 'CTL'); % get the end;
if(length(sf) >= 1) 
   QueMessage('STDP plots'); % clear the que
   % use the comparison plot figure
   hf = findobj('Tag', 'Comparison');
   if(isempty(hf))
      hf = figure('Tag', 'Comparison', 'Name', 'Comparison Plots', 'NumberTitle', 'off');
   end;
   figure(hf);
   
   clf;
   compare_plot('clear'); % clear the result summary
   QueMessage('STDP Plots', 1); % clear the que
   %extract info in a general way...
   j = 0;
   for i = 1:length(sf)
      sel = find_protocol(sf(i), 'timing_base'); % find protocol in this file
      %sel = [sf(i)-1 sf(i)+1 sf(i)+2]; % build selection assuming all the "cond's" are selected
      jc = find_protocol(sf(i), 'tb_cond');
      if(isempty(jc))
         jc = find_protocol(sf(i), 'timing_cond');
      end;
      if(~isempty(jc))
         espk = CONTROL(jc(1)).EPSPKdt;
         if(espk.shkdt >= mindt & espk.shkdt <= maxdt)
            j = j + 1;
            set(hctl, 'value', sel); % change the selection.
            fprintf(1, '%d: %s   dt=%7.2f\n', j, CONTROL(sel(1)).filename, espk.shkdt);
            compare_plot('EPSC_TC_AUTO', 1); % make the auto EPSC_TC plot
		      drawnow
            set(hctl, 'value', sf); % restore the selection
         end;
      end;
      
   end;
   set(hctl, 'value', sf); % restore the selection
   drawnow
end;

sig = TRES(2:size(TRES,1),:);
n=size(sig,1);
m = size(sig,2);
tb = TRES(1,:);
for i = 1:m
   [msig(i) ssig(i) nsig(i)] = mean_var(sig(:,i));
end;

ssig = sqrt(ssig)./sqrt(nsig);
   hf=findobj('Tag', 'Population');
   if(isempty(hf))
      hf = figure('Tag', 'Population', 'Name', 'Population Plot', 'NumberTitle', 'off');
      % checkbox for accumulation falg
   end;
   figure(hf);
   %clf;
   hck = findobj('tag', 'stdp_plots_acc_chkbox');
   if(isempty(hck) | hck == 0)
      hck = uicontrol('Units', 'normalized', ...
         'position', [0.1, 0.9, 0.15, 0.07], ...
         'horizontalalignment', 'right', ...
         'fontsize', 10, ...
         'style', 'checkbox', ...
         'String', 'Accumulate', ...
         'tag', 'stdp_plots_acc_chkbox');
   end;
   ck = findobj('tag', 'stdp_plots_acc_chkbox');
   if(isempty(ck) | ~ishandle(ck))
      chk = 0;
   else
      chk = get(ck, 'value');
   end;
   if(~chk)
      cla;
      hold off;
      subplot('Position', [0.1, 0.15, 0.8, 0.6]);
      next_color = 1; % reset the color index.
   else
      hold on
   end;
   
if(n > 2)
   % plot(tb, msig, 'marker', char(mlist(next_color)), 'color', char(clist(next_color)), 'markersize', 2);
   % hold on;
   he=my_errorbar(tb, msig, ssig, ssig, 'o', 0.3);
   hold on;
   plot(tb, msig);
else
   he=plot(tb, msig);
   % set(he, 'color', char(clist(next_color)));
   % set(he, 'marker', char(mlist(next_color)));
end;
%fprintf(1, '%d    %s\n', next_color, char(clist(next_color)));
%set(he, 'color', char(clist(next_color)));
%set(he, 'markerfillcolor', char(clist(next_color)));
%set(he, 'markersize', 2);
next_color = next_color + 1;
if(next_color > length(clist))
   next_color = 1;
end;

set(gca, 'YLim', [0 200]);
set(gca, 'XLim', [0 60]);
text(10, 50, sprintf('N=%d', n));
switch(EPSC_STATE)
case 0
   Ylabel('Peak V (normalized)');
case 1
   Ylabel('Slope (normalized)');
otherwise
end;

return;


